<?php
/**
 * Created by PhpStorm.
 * 商家充值回调处理
 * User: caicm
 * Date: 2017/6/27
 * Time: 10:28
 */

namespace WMApi\Controller;

use Com\Alipay\AlipayNotify;
use Com\Jdpay\JdpayNotify;
use Think\Controller;
use WMApi\Service\SellerRechargeService;

class SellerRechargeNotifyController extends Controller  {

    /**
     * 支付宝充值回调处理
     * Created By caicm
     */
    public function alipay_notifypay(){
        set_time_limit(0);

        $config = A('SDXApi/Alipaynotify')->get_aliyun_config();        //支付宝配置
        $alipayNotify = new AlipayNotify($config);
        $verify_result = $alipayNotify->verifyNotify();
        //验证成功
        if($verify_result) {
            $out_trade_no = $_POST['out_trade_no'];                     //商户订单号
            $trade_no = $_POST['trade_no'];                             //支付宝交易号
            $trade_status = $_POST['trade_status'];                     //交易状态

            if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {

                if(empty($_POST["total_fee"]) && !empty($_POST['total_amount'])){
                    $_POST["total_fee"] = $_POST['total_amount'];
                }

                $rechar_service = new SellerRechargeService();

                //处理回调业务
                $recharge_state = $rechar_service->recharge_notifypay(2, $out_trade_no, $trade_no, $_POST["total_fee"]);
                if($recharge_state){
                    echo "success";
                }else{
                    echo "fail";
                }
            }
        }
        else {
            //验证失败
            echo "fail";
        }
    }

    /**
     * 京东支付回调处理
     * Created By caicm
     */
    public function js_notifypay(){
        set_time_limit(0);
        $jdpayNotify = new JdpayNotify();
        $verify_result = $jdpayNotify->verifyNotify();

        //验证成功
        if($verify_result) {
            $out_trade_no = $verify_result['tradeNum'];         //商户订单号
            $trade_no = $verify_result['tradeNum'];             //交易流水号

            if($verify_result['status'] == '2' && $verify_result['result']['desc'] == 'success') {
                $total_fee = $verify_result['amount']/100;

                $rechar_service = new SellerRechargeService();

                //处理回调业务
                $recharge_state = $rechar_service->recharge_notifypay(7, $out_trade_no, $trade_no, $total_fee);
                if($recharge_state){
                    echo "success";
                }else{
                    echo "fail";
                }
            }
        }
        else {
            echo "fail";
        }
    }

}